/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package topcoder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author Mohamed.Azouz
 */
public class SRMRoomAssignmentPhase {

    public int countCompetitors(int[] ratings, int k) {
        int y = ratings[0];
        int[] nr = this.sortReverse(ratings);
        ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>();
        int i = ratings.length - 1;
        while (i >= 0) {
            for (int j = 0; j < k; j++) {
                ArrayList<Integer> element;
                try {
                    element = arrayList.get(j);
                } catch (Exception x) {
                    element = new ArrayList<Integer>();
                }
                element.add(ratings[i]);
                arrayList.set(j, element);
                i--;
                if (i < 0) {
                    break;
                }
            }
        }
        System.out.println(arrayList);
        return ratings[k - 1];
    }

    private int[] sortReverse(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int t = arr[i];
                    arr[i] = arr[j];
                    arr[j] = t;
                }
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        SRMRoomAssignmentPhase e = new SRMRoomAssignmentPhase();
        int[] ratings = {491, 981, 1199, 763, 994, 879, 888};
        int k = 3;
        System.out.println(e.countCompetitors(ratings, k));
    }
}
